﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
using System.Windows.Media.Effects;
using System.Windows.Shapes;

namespace MapSurface.WidgetSurface
{
    public class BorderElement:Grid
    {
        public Grid Content = null;
        public object objectUI = null;
        public Rectangle bottomLeftSizeGrip = null;
        public Rectangle topRightSizeGrip = null;
        public Rectangle bottomRightSizeGrip = null;
        public Rectangle topLeftSizeGrip = null;
        public Rectangle bottomSizeGrip = null;
        public Rectangle topSizeGrip = null;
        public Rectangle rightSizeGrip = null;
        public Rectangle leftSizeGrip = null;
        public WindowResizer wr = null;
        public WindowMove wm = null;
        private static int id = 0;
        private int border = 15;
        public static List<BorderElement> BorderCollection = null;
        public delegate void SelectUIhandler(object select);
        public SelectUIhandler myUISelector = null;
        private void windowResize()
        {
            wr = new WindowResizer(this);
            wr.addResizerRight(rightSizeGrip);
            wr.addResizerLeft(leftSizeGrip);
            wr.addResizerUp(topSizeGrip);
            wr.addResizerDown(bottomSizeGrip);
            wr.addResizerLeftUp(topLeftSizeGrip);
            wr.addResizerRightUp(topRightSizeGrip);
            wr.addResizerLeftDown(bottomLeftSizeGrip);
            wr.addResizerRightDown(bottomRightSizeGrip);


        }
        public void addhandler()
        {
            topLeftSizeGrip.AddHandler(Contacts.ContactTapGestureEvent,  new ContactEventHandler( delegatHandler));
            topRightSizeGrip.AddHandler(Contacts.ContactTapGestureEvent, new ContactEventHandler(delegatHandler));
            bottomLeftSizeGrip.AddHandler(Contacts.ContactTapGestureEvent, new ContactEventHandler(delegatHandler));
            bottomRightSizeGrip.AddHandler(Contacts.ContactTapGestureEvent, new ContactEventHandler(delegatHandler));
        }

        public BorderElement()
        {
        }
        public BorderElement(UIElement parent, Object content)
        {
            #region IntializeComponents
            RowDefinition r1 = new RowDefinition();
            r1.Height = new GridLength(border);
            RowDefinition r2 = new RowDefinition();
            r2.Height = GridLength.Auto;
            RowDefinition r3 = new RowDefinition();
            r3.Height = new GridLength(border);

            this.RowDefinitions.Add(r1);
            this.RowDefinitions.Add(r2);
            this.RowDefinitions.Add(r3);
            ColumnDefinition c1 = new ColumnDefinition();
            c1.Width = new GridLength(border);
            ColumnDefinition c2 = new ColumnDefinition();
            c2.Width = GridLength.Auto;
            ColumnDefinition c3 = new ColumnDefinition();
            c3.Width = new GridLength(border);

            this.ColumnDefinitions.Add(c1);
            this.ColumnDefinitions.Add(c2);
            this.ColumnDefinitions.Add(c3);
            Name = "BorderElement" + id;
            leftSizeGrip = new Rectangle();
            leftSizeGrip.Name = "leftSizeGrip" + id;
            leftSizeGrip.Fill = Brushes.Transparent;
            SetColumn(leftSizeGrip, 0);
            SetRow(leftSizeGrip, 1);
            base.Children.Add(leftSizeGrip);


            rightSizeGrip = new Rectangle();
            rightSizeGrip.Name = "rightSizeGrip" + id;
            rightSizeGrip.Fill = Brushes.Transparent;
            SetColumn(rightSizeGrip, 2);
            SetRow(rightSizeGrip, 1);
            base.Children.Add(rightSizeGrip);

            topSizeGrip = new Rectangle();
            topSizeGrip.Name = "topSizeGrip" + id;
            topSizeGrip.Fill = Brushes.Transparent;
            SetColumn(topSizeGrip, 1);
            SetRow(topSizeGrip, 0);
            base.Children.Add(topSizeGrip);


            bottomSizeGrip = new Rectangle();
            bottomSizeGrip.Name = "bottomSizeGrip" + id;
            bottomSizeGrip.Fill = Brushes.Transparent;
            SetColumn(bottomSizeGrip, 1);
            SetRow(bottomSizeGrip, 2);
            base.Children.Add(bottomSizeGrip);


            topLeftSizeGrip = new Rectangle();
            topLeftSizeGrip.Name = "topLeftSizeGrip" + id;
            topLeftSizeGrip.Fill = Brushes.Gray;
            SetColumn(topLeftSizeGrip, 0);
            SetRow(topLeftSizeGrip, 0);
            base.Children.Add(topLeftSizeGrip);


            bottomRightSizeGrip = new Rectangle();
            bottomRightSizeGrip.Name = "bottomRightSizeGrip" + id;
            bottomRightSizeGrip.Fill = Brushes.Gray;
            SetColumn(bottomRightSizeGrip, 2);
            SetRow(bottomRightSizeGrip, 2);
            base.Children.Add(bottomRightSizeGrip);


            topRightSizeGrip = new Rectangle();
            topRightSizeGrip.Name = "topRightSizeGrip" + id;
            topRightSizeGrip.Fill = Brushes.Gray;
            SetColumn(topRightSizeGrip, 2);
            SetRow(topRightSizeGrip, 0);
            base.Children.Add(topRightSizeGrip);


            bottomLeftSizeGrip = new Rectangle();
            bottomLeftSizeGrip.Name = "bottomLeftSizeGrip" + id;
            bottomLeftSizeGrip.Fill = Brushes.Gray;
            SetColumn(bottomLeftSizeGrip, 0);
            SetRow(bottomLeftSizeGrip, 2);
            base.Children.Add(bottomLeftSizeGrip);


            #endregion

            this.Margin = new Thickness(0);
            Content = new Grid();
            
            Content.Name = "BorderContent" + id;
            SetRow(Content, 1);
            SetColumn(Content, 1);
            base.Children.Add(Content);
            ((FrameworkElement)content).Margin = new Thickness(3);
            Content.Children.Add((UIElement)content);
            ((FrameworkElement)content).MinHeight = 30;
            ((FrameworkElement)content).MinWidth = 30;

            if (BorderCollection == null)
                BorderCollection = new List<BorderElement>();
            BorderCollection.Add(this);
            windowResize();
            wm = new WindowMove(parent, this);
            wr.MainCanvas = parent;
            wm.connectMouseHandlers(this);
            objectUI = content;
            id++;
        }
        public void setBorderVisible()
        {
            foreach (Object r in this.Children)
            {
                try
                {
                    if(((Rectangle)r).Fill==Brushes.Transparent)
                        ((Rectangle)r).Fill = Brushes.Blue;
                }
                catch (Exception)
                { }
            }

        }
        public void setEditMode(bool edit)
        {
            if (!edit)
            {
                bottomLeftSizeGrip.Fill = Brushes.Transparent;
                topLeftSizeGrip.Fill = Brushes.Transparent;
                bottomRightSizeGrip.Fill = Brushes.Transparent;
                topRightSizeGrip.Fill = Brushes.Transparent;
            }
            else
            {
                bottomLeftSizeGrip.Fill = Brushes.Gray;
                topLeftSizeGrip.Fill = Brushes.Gray;
                bottomRightSizeGrip.Fill = Brushes.Gray;
                topRightSizeGrip.Fill = Brushes.Gray;

            }
        }
        public void setBorderNotVisible()
        {
            foreach (Object r in this.Children)
            {
                try
                {
                    if (((Rectangle)r).Fill == Brushes.Blue)
                     ((Rectangle)r).Fill = Brushes.Transparent;
                }
                catch (Exception)
                { }
            }

        }
        public static void resetBorderElement()
        {
            id = 0;
            if (BorderCollection != null)
                while (BorderCollection.Count > 0)
                    BorderCollection.RemoveAt(0);
             BorderCollection = new List<BorderElement>();
        }
        public void replaceContentElement(object newContent)
        {
            //SetSize
            object content = this.Content.Children[0];
            Thickness t= ((FrameworkElement)content).Margin;
            ((FrameworkElement)newContent).Height = ((FrameworkElement)content).Height;
            ((FrameworkElement)newContent).Width = ((FrameworkElement)content).Width;
            ((FrameworkElement)newContent).Margin = new Thickness(t.Left, t.Top, t.Right, t.Bottom);
            this.Content.Children.Remove((UIElement)content);
            this.Content.Children.Add((UIElement)newContent);
            

        }
        private void delegatHandler(object sender, ContactEventArgs e)
        {
            bool canSelect = true;
            foreach (BorderElement b in BorderCollection)
            {
                if (b.wr.CanResize || b.wm.CanMove)
                    canSelect = false;
            }
            if (canSelect)
                myUISelector(this.Content.Children[0]);
            e.Handled = false;
        }
        public static BorderElement getBorderElement(Object element)
        {
            foreach (BorderElement b in BorderElement.BorderCollection)
            {
                if(b.Content.Children.Contains((UIElement)element))
                    return b;
            }
            return null;
        }

    }
}
